//
//  SystemDetailController.m
//  51易借
//
//  Created by 张睿 on 2017/8/30.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "SystemDetailController.h"
#import "MessageDetailTableViewCell.h"
@interface SystemDetailController ()

@property (nonatomic, weak)UIView * topView;
@property (nonatomic,strong)MessageListModel * messageDetail;
@end

@implementation SystemDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    [self setupUI];
    [self getMessageDetail];
}


- (void)setupNavBar{
    
    self.title = self.titleString;
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}



- (void)setupUI{
    
    //    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.view.backgroundColor = BACK_COLOR;
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = ColorWithRGB(0xff7907);
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.separatorStyle = NO;
    self.tableView.estimatedRowHeight = 100;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MessageDetailTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"item"];
    
}


- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.leading.trailing.mas_equalTo(self.view);
        make.top.mas_equalTo(self.topView.mas_bottom);
    }];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messageDetail?1:0;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"item" forIndexPath:indexPath];
    cell.model = self.messageDetail;
    return cell;
}

- (void)backToLastView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getMessageDetail
{
    ParaModel*para = [ParaModel new];
    para.messageId = self.model.id;
    
    [[NetworkTool shareManager] POST:[Interfacefile getMsgDetailInfo] Param:[para toDictionary] andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
            self.model.read = @(YES);
            self.messageDetail = [[MessageListModel alloc] initWithDictionary:[responseObject objectForKey:@"data"] error:nil];
        }else{
            
            [self showHint:[responseObject objectForKey:@"message"]];
            
            
        }
        [self.tableView reloadData];
    } andFailure:^(NSError * _Nonnull error) {
        [self showHint:@"网络错误"];
    }];
    
}

@end
